//
// C++ Implementation: activity
//
// Description:
//
//
// Author: Willi Ballenthin <wilbal1087@gmail.com>, (C) 2008
//
// Copyright: See COPYING file that comes with this distribution
//
//
#include "activity.h"

/**
* Constructor. Takes a reference to the database interface to use.
* @param query The database interface with which to submit/insert data.
*/
Activity::Activity (ActivityDbInterface* query)
{
    this->query = query;

    sql = new QSqlQuery;

    date = QDate::currentDate();
    time = QTime ( 12, 0, 0, 0 );
    duration = QTime ( 0, 0, 0, 0 );
    distance = 0;
    effort = 0;
    location = "Location";
    route = "Route";
    notes = "Notes";
    splits = "Splits";
    type = "Run";
    id = 0;
}

/**
* Copy constructor.
*/
Activity::Activity ( const Activity& act )
{
    date = act.date;
    time = act.time;
    duration = act.duration;
    distance = act.distance;
    location = act.location;
    type = act.type;
    route = act.route;
    notes = act.notes;
    splits = act.splits;
    effort = act.effort;
    id = act.id;
    query = act.query;
}

/**
* Operator=
*/
Activity& Activity::operator= ( const Activity& act )
{
    date = act.date;
    time = act.time;
    duration = act.duration;
    distance = act.distance;
    location = act.location;
    type = act.type;
    route = act.route;
    notes = act.notes;
    splits = act.splits;
    effort = act.effort;
    id = act.id;
    query = act.query;

    return *this;
}


/**
* Accessor.
* @return The queried value.
*/
QDate   Activity::getDate()
{
    return date;
}

/**
* Accessor.
* @return The queried value.
*/
QTime   Activity::getTime()
{
    return time;
}

/**
* Accessor.
* @return The queried value.
*/
QTime   Activity::getDuration()
{
    return duration;
}

/**
* Accessor.
* @return The queried value.
*/
float   Activity::getDistance()
{
    return distance;
}

/**
* Accessor.
* @return The queried value.
*/
QString Activity::getType()
{
    return type;
}

/**
* Accessor.
* @return The queried value.
*/
QString Activity::getLocation()
{
    return location;
}

/**
* Accessor.
* @return The queried value.
*/
QString Activity::getRoute()
{
    return route;
}

/**
* Accessor.
* @return The queried value.
*/
QString Activity::getNotes()
{
    return notes;
}

/**
* Accessor.
* @return The queried value.
*/
QString Activity::getSplits()
{
    return splits;
}

/**
* Accessor.
* @return The queried value.
*/
int     Activity::getEffort()
{
    return effort;
}

/**
* Mutator.
* @param date The value to set.
*/
void Activity::setDate ( QDate date )
{
    this->date = date;
}

/**
* Mutator.
* @param var The value to set.
*/
void Activity::setTime ( QTime var )
{
    this->time = var;
}

/**
* Mutator.
* @param var The value to set.
*/
void Activity::setDuration ( QTime var )
{
    this->duration = var;
}

/**
* Mutator.
* @param var The value to set.
*/
void Activity::setDistance ( float var )
{
    this->distance = var;
}

/**
* Mutator.
* @param var The value to set.
*/
void Activity::setLocation ( QString var )
{
    this->location = var;
}

/**
* Mutator.
* @param type The value to set.
*/
void Activity::setType ( QString type )
{
    this->type = type;
}

/**
* Mutator.
* @param var The value to set.
*/
void Activity::setRoute ( QString var )
{
    this->route = var;
}

/**
* Mutator.
* @param var The value to set.
*/
void Activity::setNotes ( QString var )
{
    this->notes = var;
}

/**
* Mutator.
* @param var The value to set.
*/
void Activity::setSplits ( QString var )
{
    this->splits = var;
}

/**
* Mutator.
* @param var The value to set.
*/
void Activity::setEffort ( int var )
{
    this->effort = var;
}

/**
* Accessor.
* @return The queried value.
*/
int Activity::getId()
{
    return id;
}

/**
* Mutator.
* TODO: make this private and activitydbinterface a friend.
* @param i The value to set.
*/
void Activity::setId ( int i )
{
    id = i;
}



/**
* Insert this activity into the database's records.
* @return True on successful addition.
*/
bool Activity::insert ()
{
    if ( id == Activity::NEW ) // this is a new activity then
    {
        // do not specify an ID
        sql->prepare ( "INSERT INTO logTable "
                        "("
                        "date, time, duration, distance, type,"
                        " location, route, notes, splits, effort"
                        ")"
                        " VALUES "
                        "("
                        ":date, :time, :duration, :distance, :type, "
                        ":location, :route, :notes, :splits, :effort"
                        ")" );
        sql->bindValue ( ":date",      date );
        sql->bindValue ( ":time",      time );
        sql->bindValue ( ":duration",  duration );
        sql->bindValue ( ":distance",  distance );
        sql->bindValue ( ":type",      type );
        sql->bindValue ( ":location",  location );
        sql->bindValue ( ":route",     route );
        sql->bindValue ( ":notes",     notes );
        sql->bindValue ( ":splits",    splits );
        sql->bindValue ( ":effort",    effort );
        if ( !sql->exec() )
        {
            qWarning ( "failed to insert activity into database" );
            return false;
        }
        return true;
    }
    else    // else we are editing a previous activity
    {
        sql->prepare ( "UPDATE logTable SET "
                        "date=:date, time=:time, duration=:duration, distance=:distance, type=:type, "
                        "location=:location, route=:route, notes=:notes, splits=:splits, effort=:effort "
                        "WHERE id=:id"
                      );
        sql->bindValue ( ":id",        id );
        sql->bindValue ( ":date",      date );
        sql->bindValue ( ":time",      time );
        sql->bindValue ( ":duration",  duration );
        sql->bindValue ( ":distance",  distance );
        sql->bindValue ( ":type",      type );
        sql->bindValue ( ":location",  location );
        sql->bindValue ( ":route",     route );
        sql->bindValue ( ":notes",     notes );
        sql->bindValue ( ":splits",    splits );
        sql->bindValue ( ":effort",    effort );
        if ( !sql->exec() )
        {
            qWarning ( "failed to update activity in database" );
            qDebug() << sql->lastQuery();
            return false;
        }
        return true;
    }
}



/**
* Delete this activity from the database's records.
* @return True on successful deletion.
*/
bool Activity::discard ()
{
    if ( id <= 0) // this is a new (or deleted) activity then, nothing to do
    {
        return true;
    }
    else    // else were editing a previous activity
    {
        sql->prepare ( "DELETE FROM logTable WHERE "
                        "id=:id");
        sql->bindValue ( ":id", id );
        if ( !sql->exec() )
        {
            qWarning ( "failed to delete activity in database" );
            qDebug() << sql->lastQuery() << "id: " << id;
            return false;
        }
        return true;
    }
}



